package org.example.ttms.customize;

import javafx.collections.FXCollections;
import org.example.ttms.controller.BuyTicketHostController;
import org.example.ttms.controller.UserController;
import org.example.ttms.entityClass.User;
import org.example.ttms.entityClass.ticket.Ticket;
import org.example.ttms.entityClass.train.Trips;

import java.util.HashMap;
import java.util.Map;

public class BuyTicket extends Window{
    public static Trips trips;
    //买票窗口
    public void buyTicketWindow(Trips trips2){
        trips = trips2;
        title = "购票";
        window("buyTicket.fxml");
    }
    //买票成功
    public void buyTicket(String type, String str){
        Ticket ticket = new Ticket();
        HashMap<String, Ticket> hashMap = ticket.obtainAllTicket();
        for(Map.Entry<String, Ticket> map : hashMap.entrySet()){
            Ticket ticket1 = map.getValue();
            if(ticket1.getId().equals(trips.getId()) && ticket1.getType().equals(type)
            && (ticket1.getStatus().equals("未售出") || ticket1.getStatus().equals("已退票"))){
                ticket1.setStatus("已售出");
                User user = UserController.user;
                Ticket ticket2 = BuyTicketHostController.ticket;
                ticket1.setBuyerName(user.getName());
                ticket1.setGoTime(ticket2.getGoTime());
                ticket1.setArrivalTime(ticket2.getArrivalTime());
                ticket1.setDeparture(ticket2.getDeparture());
                ticket1.setDestination(ticket2.getDestination());
                user.addBuyTicket(ticket1);
                hashMap.put(ticket1.getId(), ticket1);
                ticket1.storageTicket(FXCollections.observableArrayList(hashMap.values()));
                trips.setSellTicketNum(trips.getSellTicketNum()+1);
                HashMap<String, Trips> tripsHashMap = trips.obtainAllTrips3();
                tripsHashMap.put(trips.getId(), trips);
                trips.storageTrips(FXCollections.observableArrayList(tripsHashMap.values()));
                Pop_ups popUps = new Pop_ups();
                popUps.pop_ups4(str);
                break;
            }
        }
    }
}
